package com.kgc.scd.controller;

import com.kgc.scd.bean.ShopUser;
import com.kgc.scd.service.ModifyShopUserService;
import com.kgc.scd.service.SensitiveWordService;
import com.kgc.scd.util.RedisUtils;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import com.kgc.scd.util.SensitiveWordFilter;
import com.kgc.scd.vo.ShopAddressVO;
import com.kgc.scd.vo.ShopUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created On : 2022/11/16.
 * <p>
 * Author : 刘慧枫
 * <p>
 * Description: 修改个人资料入口
 */
@Slf4j
@RestController
@Api(tags = "修改个人资料入口")
public class ModifyShopUserController {

    @Autowired
    private ModifyShopUserService modifyShopUserService;

    @Autowired
    private SensitiveWordFilter sensitiveWordFilter;

    /**
     * @author : 刘慧枫
     * @date   : 2022/11/17
     * @param  : [gender, birthday, nickname, shopUser]
     * @return : com.kgc.scd.util.RequestResult<com.kgc.scd.bean.ShopUser>
     * @description : 修改个人资料
     */
    @PostMapping("/modUserInfo")
    @ApiOperation(value = "修改个人资料",notes = "基于mybatis-plus")
    public RequestResult<ShopUserVO> modUserInfo(@RequestParam(required = false) Integer gender,
                                                 @RequestParam(required = false) Date birthday,
                                                 @RequestParam(required = false) String nickname,
                                                 @RequestParam(required = false) String headImg,
                                                 @RequestBody ShopUser shopUser){
        if (nickname.equals("")){
            log.warn("------ 昵称不能为空，昵称：{} ------", nickname);
            return ResultBuildUtil.fail("998","修改失败，昵称不能为空");
        }

        // 校验搜索关键字中，是否包含敏感词，如果包含，提示错误
        if(sensitiveWordFilter.isContainSensitiveWordMin(nickname)){
            log.warn("------ 命中敏感词，昵称：{} ------", nickname);
            return ResultBuildUtil.fail("997","修改失败，命中敏感词");
        }

        return ResultBuildUtil.success(modifyShopUserService.modShopUserInfo(gender,birthday,nickname,headImg,shopUser));
    }

    /**
     * @author : 刘慧枫
     * @date   : 2022/11/17
     * @param  : [userOldPwd, userNewPwd, verifyUserNewPwd, shopUser]
     * @return : com.kgc.scd.util.RequestResult<java.util.Map<java.lang.String,java.lang.Object>>
     * @description : 修改密码
     */
    @PostMapping("/modUserPwd")
    @ApiOperation(value = "修改密码",notes = "基于mybatis-plus")
    public RequestResult<Map<String,Object>> modUserPwd(@RequestParam String userOldPwd,
                                             @RequestParam String userNewPwd,
                                             @RequestParam String verifyUserNewPwd,
                                             @RequestBody ShopUser shopUser){
        //放入map返回前端
        Map<String,Object> resultMap = new HashMap<>();
        //修改密码
        String resultStr = modifyShopUserService.modShopUserPwd(userOldPwd,userNewPwd,verifyUserNewPwd,shopUser);

        if (!resultStr.contains("成功")){
            resultMap.put("errMsg",resultStr);
            return ResultBuildUtil.fail(resultMap);
        }

        resultMap.put("userOldPwd",userOldPwd);
        resultMap.put("userNewPwd",userNewPwd);
        resultMap.put("verifyUserNewPwd",verifyUserNewPwd);
        resultMap.put("resultStr",resultStr);

        return ResultBuildUtil.success(resultMap);
    }

    /**
     * @author : 刘慧枫
     * @date   : 2022/11/17
     * @param  : [gender, birthday, nickname, shopUser]
     * @return : com.kgc.scd.util.RequestResult<com.kgc.scd.bean.ShopUser>
     * @description : 添加个人地址
     */
    @PostMapping("/addUserAddress")
    @ApiOperation(value = "添加个人地址",notes = "基于mybatis-plus")
    public RequestResult<ShopAddressVO> addUserAddress(@RequestParam String name,
                                                       @RequestParam String address,
                                                       @RequestParam String mobile,
                                                       @RequestParam Integer status,
                                                       @RequestBody ShopUser shopUser){

        if (StringUtils.isBlank(name)){
            log.warn("------ 姓名不能为空，姓名：{} ------", name);
            return ResultBuildUtil.fail("998","添加失败，姓名不能为空");
        }
        if (StringUtils.isBlank(address)) {
            log.warn("------ 地址不能为空，地址：{} ------", address);
            return ResultBuildUtil.fail("998","添加失败，地址不能为空");

        }
        if (StringUtils.isBlank(mobile)){
            log.warn("------ 手机号不能为空，手机号：{} ------", mobile);
            return ResultBuildUtil.fail("998","添加失败，手机号不能为空");
        }

        // 校验搜索关键字中，是否包含敏感词，如果包含，提示错误
        if(sensitiveWordFilter.isContainSensitiveWordMin(name)){
            log.warn("------ 命中敏感词，账户：{} ------", name);
            return ResultBuildUtil.fail("997","添加失败，命中敏感词");
        }

        //增加地址
        return ResultBuildUtil.success(modifyShopUserService.addUserAddress(name,address,mobile,status,shopUser));
    }

}
